package com.pb.infra.taskManage.controller;

import com.pb.infra.oth.util.SysLog;
import com.pb.infra.taskManage.service.ProcessManageService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 流程管理
 * @author liaohaoyuan
 * @date 20210501
 */
@Slf4j
@RestController
@RequestMapping("v1/taskManage/processManage")
public class ProcessManageController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private ProcessManageService processmanageService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "ma_job_cfg";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "流程管理";

    /**
     * 查询分页数据
     * @param paramMap 查询参数
     * @param page 页码
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> paramMap, Page page) {
        return processmanageService.select(paramMap, page);
    }

    /**
     *查询流程下拉框数据
     * @return Result 返回数据
     */
    @GetMapping("getProcedureData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getProcedureData(){
        List<Map<String, Object>> appIdList = processmanageService.getProcedureData();
        return Result.ok(appIdList);
    }

    /**
     * 保存或者新建流程
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("save")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result save(@RequestBody Map<String,Object> paramMap){
        try {
            Result insert = processmanageService.save(paramMap);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 修改流程数据
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String,Object> paramMap){
        try {
            Result update = processmanageService.update(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 删除流程数据
     * @param paramList 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramList){
        try {
            Result update = processmanageService.delete(paramList);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 启动
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("startUp")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result startUp(@RequestBody Map<String,Object> paramMap){
        try {
            Result update = processmanageService.startUp(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 暂停
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("stopTasks")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public  Result stopTasks(@RequestBody Map<String,Object> paramMap){
        try {
            Result update = processmanageService.stopTasks(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 继续
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("continueTasks")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result continueTasks(@RequestBody Map<String,Object> paramMap){
        try {
            Result update = processmanageService.continueTasks(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 重启
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("rebootings")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result rebootings(@RequestBody Map<String,Object> paramMap){
        try {
            Result update = processmanageService.rebootings(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 任务数据
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("setIntervalData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result setIntervalDat(@RequestBody Map<String,Object> paramMap){
        try {
            Result insert = processmanageService.setIntervalDat(paramMap);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

}
